<?php

declare (strict_types=1);

namespace App\Model;

/**
 * @property int $uid
 * @property string $name
 * @property string $account_id
 * @property int $account_type
 * @property string $account_front
 * @property string $account_back
 * @property string $introduce
 * @property string $language
 * @property string $education
 * @property string $education_img
 * @property string $certificate
 * @property string $certificate_img
 * @property string $direction
 * @property string $crowd
 * @property int $status
 * @property int $create_time
 * @property int $check_time
 * @property int $update_time
 * @property string $cover
 */
class ConsultantDatum extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'consultant_datum';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['uid' => 'integer', 'account_type' => 'integer', 'status' => 'integer', 'create_time' => 'integer', 'check_time' => 'integer', 'update_time' => 'integer'];

    public static function getConsultantList()
    {

        $select = [
            'a.uid',
            'c.username as num',
            'a.name as nickname',
            'a.type',
            'a.direction',
            'a.score',
            'a.praise',
            'a.hours',
            'a.language',
            'a.cover',
            'c.online',
            'c.service_status',
            'b.self_name as countryName',
            'b.country_img'
        ];


        $model = self::from('consultant_datum as a')
            ->leftJoin('country as b', 'a.country', '=', 'b.id')//国家表
            ->leftJoin('chat_user as c', 'a.uid', '=', 'c.uid')
            ->where(['a.status' => 1])
            ->select($select)
            ->orderBy('a.uid', 'desc');


        $list = page($model);


        return $list;

    }


    /**
     * 临时返回
     * @return mixed[]
     */
    public static function getRecommendConsultantList()
    {
        $select = [
            'a.uid',
            'a.name',
            'a.type',
            'a.direction',
            'a.score',
            'a.praise',
            'a.hours',
            'a.language',
            'a.cover',
            'c.online',
            'c.service_status',
            'b.self_name as countryName',
            'b.country_img'
        ];


        $list = self::from('consultant_datum as a')
            ->leftJoin('country as b', 'a.country', '=', 'b.id')//国家表
            ->leftJoin('chat_user as c', 'a.uid', '=', 'c.uid')
            ->where(['a.status' => 1])
            ->select($select)
            ->limit(3)
            ->orderBy('a.uid', 'desc')->get()->toArray();


        return $list;
    }


    /**
     * 搜索咨询师 简单版
     * @param $keyword
     * @return array
     */
    public static function searchConsultantList($keyword)
    {

        $where[] = ['a.status', '=', 1];
        $where[] = ['a.name', 'like', "%$keyword%"];//咨询师姓名
        $where[] = ['a.education', 'like', "%$keyword%", 'or'];//教育学历
        $where[] = ['a.certificate', 'like', "%$keyword%", 'or'];//证书
        $where[] = ['a.direction_str', 'like', "%$keyword%", 'or'];//擅长方向标签
        $where[] = ['a.introduce', 'like', "%$keyword%", 'or'];//个人简介里面内容匹配

        $select = [
            'a.uid',
            'a.name',
            'a.type',
            'a.direction',
            'a.score',
            'a.praise',
            'a.hours',
            'a.language',
            'a.cover',
            'c.online',
            'c.service_status',
            'b.self_name as countryName',
            'b.country_img'
        ];


        $model = self::from('consultant_datum as a')
            ->leftJoin('country as b', 'a.country', '=', 'b.id')//国家表
            ->leftJoin('chat_user as c', 'a.uid', '=', 'c.uid')
            ->where($where)
            ->select($select)
            ->orderBy('a.uid', 'desc');

        return page($model);
    }


    /**
     * 获取咨询师基本信息
     * @param $uid
     * @return array|null
     */
    public static function consultantBaseInfo($uid)
    {
        $lang = getLang();
        $select = [
            'a.uid',
            'a.name as nickname',
            'a.type',
            'a.direction',
            'a.introduce',
            'a.education',
            'a.crowd',
            'a.certificate',
            'a.score',
            'a.praise',
            'a.people',
            'a.hours',
            'a.years',
            'a.language',
            'a.cover',
            'b.self_name as countryName',
            'b.country_img',
            'c.name_'.$lang.' as educationName'

        ];


        $info = self::from('consultant_datum as a')
            ->leftJoin('country as b', 'a.country', '=', 'b.id')//国家表
            ->leftJoin('education_level as c','a.education','=','c.id')//学历表
            ->where(['a.uid' => $uid, 'a.status' => 1])
            ->select($select)
            ->first();

        if ($info) {
            return $info->toArray();
        } else {
            return null;
        }
    }


    public function getDirectionAttribute($value)
    {

        //todo 规则整理好后使用redis的国家固定数据
        $temp = explode(',', $value);
        $cate = null;
        if (!empty($temp)) {

            $lang = getLang();

            $cate = ConfusionCategory::whereIn('id', $temp)->where(['lang' => $lang, 'status' => 1])->pluck('name');

        }

        return $cate;
    }


    public function getLanguageAttribute($value)
    {


        //todo 整理完成之后 redis 固定语言列表

        $temp = explode(',', $value);
        $language = null;

        if (!empty($temp)) {

            $language = Language::whereIn('id', $temp)->pluck('name');

        }

        return $language;
    }


    public function getCrowdAttribute($value)
    {


        //todo 整理完成之后 redis 固定语言列表

        $temp = explode(',', $value);
        $crowd = null;

        if (!empty($temp)) {

            $lang = getLang();

            $crowd = CrowdCategory::whereIn('id', $temp)->where(['lang' => $lang, 'status' => 1])->pluck('name');

        }

        return $crowd;
    }


}
